package com.jay.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings

/**
 *
 * @author zhouxu
 * @email 1419982188@qq.com
 * @date 2021/7/5
 */
object IntentUtils {

    open fun startPermission(context: Context, packageName: String) {
        val SCHEME = "package"
        //调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.1及之前版本)
        val APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName"
        //调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.2)
        val APP_PKG_NAME_22 = "pkg"
        //InstalledAppDetails所在包名
        val APP_DETAILS_PACKAGE_NAME = "com.android.settings"
        //InstalledAppDetails类名
        val APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails"

        val intent = Intent()
        val apiLevel = Build.VERSION.SDK_INT
        if (apiLevel >= 9) { // 2.3（ApiLevel 9）以上，使用SDK提供的接口
            intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            val uri: Uri = Uri.fromParts(SCHEME, packageName, null)
            intent.data = uri
        } else { // 2.3以下，使用非公开的接口（查看InstalledAppDetails源码）
            // 2.2和2.1中，InstalledAppDetails使用的APP_PKG_NAME不同。
            val appPkgName = if (apiLevel == 8) APP_PKG_NAME_22 else APP_PKG_NAME_21
            intent.action = Intent.ACTION_VIEW
            intent.setClassName(
                APP_DETAILS_PACKAGE_NAME,
                APP_DETAILS_CLASS_NAME
            )
            intent.putExtra(appPkgName, packageName)
        }
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(intent)
    }
}